package com.lyfei.yd.system.domain.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.core.type.TypeReference;
import com.lyfei.yd.common.utils.GenericAndJson;
import com.lyfei.yd.common.utils.StringUtils;
import com.lyfei.yd.system.domain.BaseEntity;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@TableName("t_sku")
public class Sku extends BaseEntity<Sku> {
    private BigDecimal price;
    private BigDecimal discountPrice;
    private Boolean online;
    private String img;
    private String title;
    private Long spuId;
    private Long categoryId;
    private Long rootCategoryId;
    private String  specs;
    private String code;
    private Integer stock;
    private Date createTime;
    private Date updateTime;

    @TableLogic
    private Boolean deleted;

    @TableField(exist = false)
    private List<SpecKeyValue> specKeyValues;

    public List<SpecKeyValue> getSpecKeyValues() {
        // 解析规格字符串
        if(StringUtils.isEmpty(specs)) {
            return Collections.emptyList();
        }

        return GenericAndJson.jsonToObject(this.specs, new TypeReference<List<SpecKeyValue>>(){});
    }
}
